package com.shelpe.services.tenant.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.shelpe.services.tenant.dao.PurchaseOrderMapper;
import com.shelpe.services.tenant.model.PurchaseOrder;
import com.shelpe.services.tenant.model.PurchaseOrderExample;
import com.shelpe.services.tenant.service.OrderService;

@Service
public class OrderServiceImpl implements OrderService{

	private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);
	
	@Autowired
	private PurchaseOrderMapper orderMapper;

	@Override
	public PurchaseOrder parseOrderContent(String content) {
		// TODO Auto-generated method stub
		JSONObject object = JSON.parseObject(content);
		PurchaseOrder order = new PurchaseOrder();
		
		order.setArticleCode(object.getString("article_code"));
		order.setArticleName(object.getString("article_name"));
		order.setBizOrderId(object.getLong("biz_order_id"));
		order.setBizType(Integer.valueOf(object.getString("biz_type")));
		logger.info(">>>parseOrderContent orderId {}, createTimeStr {}, createTime {}", object.getLong("order_id"), object.getString("create"), object.getDate("create"));
		order.setCreateTime(object.getDate("create"));
		order.setFee(Float.valueOf(object.getString("fee")));
		order.setItemCode(object.getString("item_code"));
		order.setItemName(object.getString("item_name"));
		order.setNick(object.getString("nick"));
		order.setOrderCycle(Integer.valueOf(object.getString("order_cycle")));
		order.setOrderCycleEnd(object.getDate("order_cycle_end"));
		order.setOrderCycleStart(object.getDate("order_cycle_start"));
		order.setOrderId(object.getLong("order_id"));
		order.setOrderStatus(object.getInteger("order_status"));
		order.setPayStatus(object.getInteger("pay_status"));
		order.setPromFee(Float.valueOf(object.getString("prom_fee")));
		order.setRefundFee(Float.valueOf(object.getString("refund_fee")));
		order.setTotalPayFee(Float.valueOf(object.getString("total_pay_fee")));
		order.setVersionNo(object.getInteger("version_no"));
		
		return order;
	}

	@Override
	public int insertOrder(PurchaseOrder order) {
		// TODO Auto-generated method stub
		return orderMapper.insert(order);
	}

	@Override
	public PurchaseOrder getOrderById(Long orderId) {
		// TODO Auto-generated method stub
		return orderMapper.selectByPrimaryKey(orderId);
	}

	@Override
	public int countById(Long orderId) {
		// TODO Auto-generated method stub
		PurchaseOrderExample example = new PurchaseOrderExample();
		example.createCriteria().andOrderIdEqualTo(orderId);
		
		return orderMapper.countByExample(example);
	}

	@Override
	public int deleteOrderById(Long orderId) {
		// TODO Auto-generated method stub
		return orderMapper.deleteByPrimaryKey(orderId);
	}
	

}
